/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.passport.controller  
 */
package com.apache.passport.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.action.PlatFromAction;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.database.model.Page;
import com.apache.passport.common.DesUtils;
import com.apache.passport.common.PassportHelper;
import com.apache.passport.entity.BlackRoster;
import com.apache.passport.entity.Token;
import com.apache.passport.manager.BlackRosterManager;
import com.apache.tools.StrUtil;

/**
 * description:  黑名单管理
 * @author Hou Dayu 创建时间：2014-12-24  
 * @Copyright(c)2014:
 */
@Controller
@RequestMapping({ "/passport/black/" })
public class BlackRosterAction extends PlatFromAction<BlackRoster> {
	private final String BLACK_LIST = getPrefix() + "black-list";
	private final String BLACK_SAVE = getPrefix() + "black-save";
	@Autowired
	private BlackRosterManager blackRosterManager;

	@RequestMapping({ "black-list.action" })
	public ModelAndView initList(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(BLACK_LIST);
		return view;
	}

	@RequestMapping({ "black-save.action" })
	public ModelAndView save(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(BLACK_SAVE);
		return view;
	}

	/**
	 * TODO 执行用户修改之前,需要对数据进行反显,故首先获取数据值
	 * @see com.apache.action.PlatFromAction#edit(javax.servlet.http.HttpServletRequest, java.lang.String)
	 */
	@RequestMapping({ "edit.action" })
	public ModelAndView edit(HttpServletRequest request, String sysId) {
		ParamsVo vo = new ParamsVo();
		vo.setInfoId(sysId);
		ModelAndView view = new ModelAndView(BLACK_SAVE);
		view.addObject("item", blackRosterManager.getInfoById(vo));
		view.addObject("isAdd", "false");
		return view;
	}

	/**
	 * TODO 执行用户删除操作 
	 * @see com.apache.action.PlatFromAction#delete(javax.servlet.http.HttpServletRequest, java.lang.String)
	 */
	@RequestMapping({ "del.action" })
	@ResponseBody
	public ResultMsg delete(HttpServletRequest request, String sysId) {
		ResultMsg rmsg = new ResultMsg("F", "操作失败,请重试!");
		try {
			String remoteIp = request.getRemoteAddr();
			String userEname = request.getParameter("userEname");
			if (StrUtil.isNotNull(sysId)) {
				ParamsVo auVo = new ParamsVo();
				BlackRoster au = new BlackRoster();
				au.setBrId(sysId);
				auVo.setObj(au);
				List currSys = blackRosterManager.getList(auVo);
				if (Validator.isEmpty(currSys)) {
					rmsg.setFlag("H");
					rmsg.setMsg("当前IP不存在");
				} else {
					ParamsVo asVo = new ParamsVo();
					asVo.setInfoId(sysId);
					blackRosterManager.deleteInfo(asVo);
					SystemTools.getInstance().getCache("loginErrToken").removeCacheObject(remoteIp + userEname);
					rmsg.setFlag("T");
					rmsg.setMsg("删除成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rmsg;
	}

	@RequestMapping(value = { "save.action" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public ResultMsg save(HttpServletRequest request, HttpServletResponse response, BlackRoster blackRoster) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功!");
		String isAdd = getDefaultStr(request.getParameter("isAdd"), "true");
		ParamsVo<BlackRoster> vo = new ParamsVo<BlackRoster>();
		String cookieValue = PassportHelper.getInstance().getCurrCookie(request);
		Token token = (Token) SystemTools.getInstance().getCache("loginToken").getCacheCloneByKey(cookieValue);
		String currEname = DesUtils.getInstance().decrypt(token.getUserEname());
		blackRoster.setUserEname(Validator.isNull(currEname) ? "" : currEname);//添加系统人名称
		String nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		blackRoster.setBrCreatetime(nowDate);
		//如果系统Id为空
		if (Validator.isNull(blackRoster.getBrId())) {
			// item.setTimestampCreate(Long.valueOf(System.currentTimeMillis()));
			// item.setUserIdCreate("")
		} else {
			//添加
			isAdd = "false";
		}

		vo.setObj(blackRoster);
		String id = "";
		boolean mark = false;
		if (("false".equals(isAdd)) && (blackRoster != null))
			mark = this.blackRosterManager.editInfo(vo);
		else {
			boolean flag = false;
			//操作前首先查询IP是否存在
			//			ParamsVo<BlackRoster> vo2 = new ParamsVo<BlackRoster>();
			//			vo2.setMethodKey("containsIp");
			//			vo2.setParams("brIpaddress", blackRoster.getBrIpaddress());
			//			boolean flag = (Boolean) blackRosterManager.execute(vo2);
			if (flag) {
				rmsg.setFlag("F");
				rmsg.setMsg("此IP已经存在于黑名单列表");
				return rmsg;
			} else {
				id = this.blackRosterManager.saveInfo(vo);
			}

		}
		boolean a1 = (("false".equals(isAdd)) && (!(mark)));
		boolean a2 = (("true".equals(isAdd)) && (isNull(id)));
		if (a1 || a2) {
			rmsg.setFlag("F");
			rmsg.setMsg("操作失败！");
		}
		return rmsg;
	}

	/**
	 * description:  查询黑名单列表
	 * @param request
	 * @param response
	 * @param user
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-23
	 */
	@RequestMapping({ "list!data.action" })
	@ResponseBody
	public Object list(HttpServletRequest request, String page, String rows) {
		ParamsVo<BlackRoster> vo = new ParamsVo<BlackRoster>();
		BlackRoster blackRoster = new BlackRoster();
		vo.setParams("pageSize", Validator.getDefaultStr(rows, "20"));
		vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
		vo.setObj(blackRoster);
		Page pageList = blackRosterManager.getPageInfo(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", Integer.valueOf(pageList.getCount()));
		mapData.put("rows", pageList.getPageObjects());
		return mapData;
	}

	@Override
	protected Object data(HttpServletRequest arg0, String arg1, String arg2) {
		return null;
	}

	@Override
	protected BaseManager<BlackRoster> getBaseManager() {
		return null;
	}

	@Override
	protected String getPrefix() {
		return "/passport/passport-";
	}

}
